package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.pojo.TbAddress;
import com.guigu.qingcheng.service.TbAddressService;
import com.guigu.qingcheng.utils.PageUtil;
import com.guigu.qingcheng.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
*
* @author Jiang
* @date 2020/08/05
*/
@Slf4j
@RestController
@RequestMapping("/user/address")
@Api(tags = "TbAddressController")
public class TbAddressController {
    @Autowired
    private TbAddressService tbAddressService;

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") Integer id) {

        TbAddress tbAddress = tbAddressService.findById(id);
        return R.ok().data("tbAddress",tbAddress);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public R list() {
        List<TbAddress>  list = tbAddressService.queryAll();
        return R.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages/{pageNum}/{pageSize}")
    public R listPages(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageUtil<TbAddress> pageUtils = tbAddressService.findListAll(pageNum, pageSize);

        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public R pageQueryByAll(@RequestBody TbAddress tbAddress, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<TbAddress> pageUtils = tbAddressService.pageQueryByAll(pageNum, pageSize, tbAddress);
        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/add")
    public R add(@RequestBody TbAddress tbAddress) {

        boolean b = tbAddressService.saveTbAddress(tbAddress);
        if(b){
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public R update(@RequestBody TbAddress tbAddress) {
        boolean b = tbAddressService.updateTbAddress(tbAddress);

        if(b){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        boolean b = tbAddressService.deleteById(id);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation(value = "根据用户名查询收货地址")
    @GetMapping("/findAddressList")
    public List<TbAddress> findAddressList(String username) {
       List<TbAddress> list= tbAddressService.findAddressList(username);
        return list;
    }
    //修改默认地址
    @ApiOperation(value = "修改默认地址")
    @GetMapping("/updatetoAddress/{username}/{id}")
    public R updatetoAddress(@PathVariable("username")String username,@PathVariable("id") Integer id){
        boolean b = tbAddressService.updateAddress(username, id);

        if(b){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }
}
